package com.basic.emg.service.impl;


import com.basic.common.exception.business.BusinessException;
import com.basic.common.exception.business.ValidationException;
import com.basic.emg.domain.EmergencyMaterial;
import com.basic.emg.domain.EmergencyTeam;
import com.basic.emg.mapper.EmergencyMaterialMapper;
import com.basic.emg.service.EmergencyMaterialService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class EmergencyMaterialServiceImpl implements EmergencyMaterialService {

    @Autowired
    private EmergencyMaterialMapper materialMapper;



    @Override
    public void createMaterial(EmergencyMaterial material) {
        if(StringUtils.isBlank(material.getMatName())){
            throw new ValidationException("物资名称不能为空");
        }
        if(StringUtils.isBlank(material.getMatRef())){
            throw new ValidationException("物资编码不能为空");
        }
        if(StringUtils.isBlank(material.getMatClass())){
            throw new ValidationException("物资分类不能为空");
        }
        if(StringUtils.isBlank(material.getMatStatus())){
            throw new ValidationException("物资状态不能为空");
        }
        if(StringUtils.isBlank(material.getMatUnit())){
            throw new ValidationException("物资单位不能为空");
        }
        if(StringUtils.isBlank(material.getChargeDept())){
            throw new ValidationException("物资部门不能为空");
        }
        material.setCreateTime(new Date());
        material.setUpdateTime(new Date());
        material.setDelFlag("0");
        int i = materialMapper.insertSelective(material);
        if (i != 1){
            throw new BusinessException("创建失败！");
        }

    }

    @Override
    public List<EmergencyMaterial> selectMaterialList(EmergencyMaterial mat) {
        Example example = new Example(EmergencyMaterial.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(mat.getMatName())){
            criteria.andEqualTo("matName",mat.getMatName());
        }
        if(StringUtils.isNotBlank(mat.getMatRef())){
            criteria.andEqualTo("matRef", mat.getMatRef());
        }
        if(StringUtils.isNotBlank(mat.getMatType())){
            criteria.andEqualTo("matType",mat.getMatType());
        }
        if(StringUtils.isNotBlank(mat.getChargeDept())){
            //criteria.andEqualTo("chargeDept",mat.getChargeDept());
            criteria.andLike("chargeDept",'%' + mat.getChargeDept() + '%');

        }
        if(StringUtils.isNotBlank(mat.getMatStatus())){
            criteria.andEqualTo("matStatus",mat.getMatStatus());
        }
        if(StringUtils.isNotBlank(mat.getMatClass())){
            criteria.andEqualTo("matClass",mat.getMatClass());
        }
        if(StringUtils.isNotBlank(mat.getMatModel())){
            criteria.andEqualTo("matModel",mat.getMatModel());
        }
        if(mat.getStartTime1()!=null){
            criteria.andGreaterThanOrEqualTo("updateTime",mat.getStartTime1());
        }
        if(mat.getEndTime1()!=null){
            criteria.andLessThanOrEqualTo("updateTime",mat.getEndTime1());
        }
        criteria.andNotEqualTo("delFlag","1");

        List<EmergencyMaterial> emergencyMaterials = materialMapper.selectByExample(example);
        return emergencyMaterials;
    }

    @Override
    public EmergencyMaterial selectMaterialById(Long id) {
        EmergencyMaterial material = materialMapper.selectByPrimaryKey(id);
        if (material != null && material.getDelFlag().equals("1")){
            return null;
        }
        return material;
    }

    @Transactional
    @Override
    public void editMaterial(EmergencyMaterial material) {
        material.setUpdateTime(new Date());
        int i = materialMapper.updateByPrimaryKeySelective(material);
        if (i != 1){
            throw new BusinessException("更新失败！");
        }
    }

    @Transactional
    @Override
    public void deleteMaterial(List<Long> ids) {

        if(CollectionUtils.isEmpty(ids)){
            throw new ValidationException("id不能为空");
        }
        for(Long id : ids){
            EmergencyMaterial material = new EmergencyMaterial();
            material.setId(id);
            material.setDelFlag("1");
            materialMapper.updateByPrimaryKeySelective(material);
        }

    }
}
